package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.car.actionform.CarPendingLiftActionForm;
import com.tianqu.tms.www.car.controller.CarStockTransferConfirmController;
import com.tianqu.tms.www.car.viewhelper.CarPendingLiftViewHelper;

/**
 * @author mufeiyu
 * 库存移库 - 信息查看action
 */
public class CarStockTransferConfirmAction extends TmsAction {
	private Logger logger = Logger.getLogger(CarStockTransferConfirmAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		CarPendingLiftActionForm registerForm = (CarPendingLiftActionForm) uc.getInputForm();
		
		if(StringUtil.isNullOrBlank(registerForm.getId())){
			if (StringUtil.isNullOrBlank(registerForm.getVin()) && StringUtil.isNullOrBlank(registerForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				return mapping.getInputForward();
			}

			if (!StringUtil.isNullOrBlank(registerForm.getVin()) && !StringUtil.isNullOrBlank(registerForm.getLicensePlate())) {
				setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
				return mapping.getInputForward();
			}

			//车牌5位check
			if (!StringUtil.isNullOrBlank(registerForm.getLicensePlate()) && registerForm.getLicensePlate().length() !=5) {
				setErrorsToRequest(request, TmsConstants.U8009, null); //请输入车牌号码后5位。
				return mapping.getInputForward();
			}
			//vin 6位check
			if (!StringUtil.isNullOrBlank(registerForm.getVin()) && registerForm.getVin().length() !=6) {
				setErrorsToRequest(request, TmsConstants.U8010, null); //请输入车架号后6位。
				return mapping.getInputForward();
			}
		}
		
		CarStockTransferConfirmController controller = new CarStockTransferConfirmController();
		controller.execute(uc);
		//
		CarPendingLiftViewHelper viewhelper = new CarPendingLiftViewHelper();
		viewhelper = (CarPendingLiftViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		viewhelper.setIsPush(1);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		if (viewhelper.getConfirmFlag() == 1) {
			setErrorsToRequest(request, TmsConstants.U0039, null);
			return mapping.getInputForward();
		}else if(viewhelper.getConfirmFlag() == 2){
			mapping.findForward(TmsConstants.SUCCESS);
		}else if(viewhelper.getConfirmFlag() == 3){
			uc.setAttribute("modifyFlg", "true");
			return mapping.getInputForward();
		}else{
			request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			return mapping.getInputForward();
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}